package com.wbb.controller;

import com.wbb.result.JsonResult;
import com.wbb.service.SpecService;
import com.wbb.util.pageList;
import com.wbb.util.queryParams;
import com.wbb.vo.SpecificationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    private SpecService specService;

    @PostMapping("/queryPage")
    public JsonResult<pageList<SpecificationVo>> queryPage(@RequestBody queryParams<SpecificationVo> params){
        pageList<SpecificationVo> pageList = specService.queryPage(params);
        return JsonResult.ok().setData(pageList);
    }

    @DeleteMapping("/deleteById/{id}")
    public JsonResult<SpecificationVo> deleteById(@PathVariable Long id){
        specService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/addSpec")
    public JsonResult<SpecificationVo> addSpec(@RequestBody SpecificationVo specVo){
        specService.addSpec(specVo);
        return JsonResult.ok();
    }

    @GetMapping("/selectSpecByOptionId/{id}")
    public JsonResult<SpecificationVo> selectSpecByOptionId(@PathVariable("id") Long id){
        SpecificationVo specificationVo = specService.selectSpecByOptionId(id);
        return JsonResult.ok().setData(specificationVo);
    }

    @PostMapping("/save")
    public JsonResult<SpecificationVo> save(@RequestBody SpecificationVo specVo){
        if (specVo.getId() !=null){
            specService.updateSpec(specVo);
        }else {
            specService.addSpec(specVo);
        }
        return JsonResult.ok();
    }

    @PatchMapping("/batchSpec")
    public JsonResult<SpecificationVo> batchSpec(@RequestBody List<Long> ids){
        specService.batchSpec(ids);
        return JsonResult.ok();
    }

    @GetMapping("/getAll")
    public JsonResult<List<SpecificationVo>> getAll(){
        List<SpecificationVo> voList = specService.getAll();
        return JsonResult.ok().setData(voList);
    }
}
